<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8" />
		<title></title>
		<link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css" media="all">
	</head>
	<style type="text/css">
		.layui-form {
			margin: 15px 0;
		}
		
		.layui-form-label {
			width: 100px;
			text-align: left;
		}
		
		.layui-tab {
			border: 1px solid #00F7DE;
		}
		
		.tab-ul {
			width: 100%;
		}
		
		.tab-ul li dd {
			/*width: 13.28%;*/
			height: 60px;
			float: left;
			flex: 1;
			/*border: 1px solid #007DDB;*/
			;
		}
		
		.tab-ul-title {
			height: 40px;
			width: 100%;
			text-align: center;
			line-height: 40px;
			font-size: 16px;
			font-weight: bold;
			/*border: 1px solid #007DDB;*/
			border-bottom: 0
		}
		
		.week-title td {
			text-align: center;
		}
		
		.layui-table td {
			padding: 0px!important;
		}
		
		input[type='text'] {
			/*width: 80px;*/
			margin: 5px auto;
		}
		
		.ri,
		.ri-w {
			padding: 19px 20px;
			/*width: 100%;
			height: 100%;	*/
		}
		
		.ri-w {
			color: #CCCCCC;
		}
		
		.batch {
			float: right;
			margin: 0px 25px 0 10px;
		}
		
		.emptys {
			color: #00F7DE;
		}
		
		#ceshi {
			/*width: 100%;
			height: 100%;*/
			display: none;
		}
		
		.bj {
			background: #00F7DE;
		}
		
		.tas {
			overflow: hidden
		}
		
		.admins {
			margin-left: 0;
			text-align: center;
		}
		
		.layui-table tbody tr:hover {
			background-color: ' ' !important;
		}
		.titl{
			    padding: 5px 15px;
				width: 100%;
				text-align: center;
				font-size: 16px;
		}
		.light_star:after{
			content:'*';
			width: 0;
			height: 0;
			color: red;

		}
	</style>

	<body>

		<div class="layui-container">

			<form class="layui-form" action="">
				<input type="hidden" name="goods_id" id="goods_id" value="{$goods.goods_id}" />
				<ul class="">

					<li>
						<div class="layui-form-item">
							<label class="layui-form-label" style="margin:5px 0">套餐售卖时间:</label>
							<div class="layui-input-inline layui-col-md5">
								<input type="text" id="starttime" name="starttime" value="{$starttime}" placeholder="开始时间" class="layui-input x" />
							</div>
							<div style=" text-align: center;line-height: 38px;width: 25px;height: 38px;float: left;margin:5px 10px 5px 0;">至</div>
							<div class="layui-input-inline layui-col-md6">
								<input type="text" id="endtime" name="endtime" value="{$endtime}" placeholder="结束时间" class="layui-input x" />
							</div>
							<div class="layui-input-inline layui-col-md6"style="width:30%;">
								<input type="button" id="button" style="margin: 5px auto;" value="确认" placeholder="结束时间" class="layui-btn" />
								<input type="button" style="margin: 5px auto;" value="刷新" onClick="window.location.reload();" class="layui-btn" />
								<input type="button" style="margin: 5px auto;" value="返回上一页" onClick="javascript :history.back(-1);" class="layui-btn" />
								<a href="{:url('admin/goods/showspecs')}?id={$_GET['id']}" class="a_menu">
								<input type="button" style="margin: 5px auto;" value="查看套餐" onClick="" placeholder="结束时间" class="layui-btn" />
								</a>
							</div>
						</div>

					</li>
					<li>
						<div class="layui-form-item" style="margin-bottom: 0px;">
                            <label class="layui-form-label" style="text-align: left;">套餐设置:</label>
                            <label class="layui-form-label" style="text-align: left;width:50%;">出售时间 : {$starttime} ~ {$endtime}</label>
						</div>
						<div class="layui-tab" lay-filter="test1">
							<div id="tab-title">
								<ul class="layui-tab-title Spec">

								</ul>
							</div>

							<div class="layui-tab-content content" style="overflow: auto;">

							</div>

						</div>

					</li>

				</ul>

			</form>

			<!--批量添加的弹层-->
			<div id="ceshi">
				<form class="layui-form" action="" method="post">
					<input type="hidden" id="specs_ids" value="" />
					<ul>
						<li class="time-content">
							<div class="layui-form-item shijian" style="margin: 0;">
                        <label class="layui-form-label">选择时间段:<br><span style="font-size: 12px;">(时间为当地时间)</span></label>
								<div class="layui-input-inline layui-col-md5">
									<input type="text" id="" name="t—starttime" value="" placeholder="开始时间" class="layui-input x" />
								</div>
								<div style=" text-align: center;line-height: 38px;width: 25px;height: 38px;float: left;margin-right: 10px;">至</div>
								<div class="layui-input-inline layui-col-md6">
									<input type="text" id="" name="t—endtime" value="" placeholder="结束时间" class="layui-input x" />
								</div>
								<div class="layui-col-md1"><i class="layui-icon layui-icon-add-1 addtime" style="font-size: 40px;">&#xe654;</i></div>
							</div>

						</li>
						<li>

							<div class="layui-form-item" style="margin: 0;">
								<label class="layui-form-label">&nbsp;</label>
								<div class="layui-input-inline layui-col-md12">
									在以上时间段中选择：
								</div>
							</div>

						</li>
						<li>
							<div class="layui-form-item ">
								<label class="layui-form-label">&nbsp;</label>
								<div class="layui-input-block layui-col-xs6" style="margin: 0;">
									<input type="radio" lay-filter="radio" id="" title="仅单日" name="radio" value="1" />
									<input type="radio" lay-filter="radio" id="" title="仅双日" name="radio" value="2" />
									<input type="radio" lay-filter="radio" id="" title="按星期" name="radio" value="3" />
								</div>
								<div class="layui-input-block layui-col-xs8" id="chx" style="margin: 0;display: none;">
									<input type="checkbox" lay-skin="primary" lay-filter="xq" id="" title="每周一" name="week" value="1" />
									<input type="checkbox" lay-skin="primary" lay-filter="xq" id="" title="每周二" name="week" value="2" />
									<input type="checkbox" lay-skin="primary" lay-filter="xq" id="" title="每周三" name="week" value="3" />
									<input type="checkbox" lay-skin="primary" lay-filter="xq" id="" title="每周四" name="week" value="4" />
									<input type="checkbox" lay-skin="primary" lay-filter="xq" id="" title="每周五" name="week" value="5" />
									<input type="checkbox" lay-skin="primary" lay-filter="xq" id="" title="每周六" name="week" value="6" />
									<input type="checkbox" lay-skin="primary" lay-filter="xq" id="" title="每周日" name="week" value="0" />
								</div>

							</div>
						</li>
						<li>
							<label class="layui-form-label">&nbsp;</label>
							<!--<div class="layui-input-block layui-col-xs12" style="margin: 0;">
								&nbsp;
						</div>-->
							<div class="layui-input-block layui-col-xs8">
								<table class="layui-table">
									<thead>
										<tr>
											<th>名称</th>
											<th>价格</th>
											<th>数量</th>
											<th>最低下单数</th>
										</tr>
									</thead>
									<tbody class="batch_person">
										<!--<tr>
											<th>成人价格</th>
											<th><input type="number" name="" id="" class="layui-input" placeholder="成人价格" value="" /></th>
											<th><input type="number" name="" id="" class="layui-input" placeholder="数量" value="" /></th>
											<th><input type="number" name="" id="" class="layui-input" placeholder="最低下单" value="" /></th>
										</tr>
										<tr>
											<th>儿童价格</th>
											<th><input type="number" name="" id="" class="layui-input" placeholder="儿童价格" value="" /></th>
											<th><input type="number" name="" id="" class="layui-input" placeholder="数量" value="" /></th>
											<th><input type="number" name="" id="" class="layui-input" placeholder="最低下单" value="" /></th>
										</tr>-->
									</tbody>

								</table>
							</div>
						</li>

					</ul>
				</form>
			</div>
		</div>
	</body>
	<script src="__PUBLIC__/layui/layui.js"></script>
	<script src="__PUBLIC__/jquery/jquery.min.js"></script>
	<script type="text/javascript">
		layui.use(['layer', 'element', 'form', 'laydate'], function() {
			//http://k.com/index.php
			//规格获取
			var specsurl = '/admin/goods/werk'
			//人群获取
			var posurl = '/admin/goods/pos'
			//提交选项卡
			var editurl = '/admin/goods/addperson'
			//初始化页面未提交 点击确认按钮设置为1
			var is_update = 0;
            //获取数据
            var id = {$goods.goods_id};
            var starttime = "{$starttime}";
            var endtime = "{$endtime}";
			var personprice = {};
            var list = {$data};
            var layer = layui.layer,
				element = layui.element,
				form = layui.form,
				laydate = layui.laydate;

			//批量初始化时间控件
			$('.x').each(function() {
				laydate.render({
					elem: this //指定元素
				});
			});
            $.ajax({
                url:"/admin/goods/personprice",
                type:"POST",
                dataType:'json',
                async:false,
                data:{id:id,sdate:starttime,edate:endtime},
                success:function (data) {
                    personprice = data.data;
                }
            })

			//点击加号添加时间框
			$(document).on('click', '.addtime', function() {
				var str = '<div class="layui-form-item shijian" style="margin: 0;">';
				str += '<label class="layui-form-label">&nbsp;</label>' +
					'<div class="layui-input-inline layui-col-md5">' +
					'<input type="text" id="" name="t—starttime" value="" placeholder="开始时间" class="layui-input x" />' +
					'</div>' +
					'<div style=" text-align: center;line-height: 38px;width: 25px;height: 38px;float: left;margin-right: 10px;">至</div>' +
					'<div class="layui-input-inline layui-col-md6">' +
					'<input type="text" id="" name="t—endtime" value="" placeholder="结束时间" class="layui-input x" />' +
					'</div>' +
					'<div class="layui-col-md1" ><i class="layui-icon layui-icon-delete deltime" style="font-size: 40px;"></i></div>' +
					'</div>';
				$('.time-content').append(str);
				//重新渲染时间控件		
				$('.x').each(function() {
					laydate.render({
						elem: this //指定元素
					});
				});
			})
			$(document).on('click', '.deltime', function() {

				$(this).parent().parent().remove()

			})

			$(document).on('click', '.emptys', function() {

				var specs = $(this).attr('date-pid')
				var tr = $('#yue' + specs + ' .tabto ._person tr');
				layer.msg('取消后无法恢复 请谨慎操作', {
					time: 0 //不自动关闭
						,
					btn: ['确定', '取消'],
					yes: function(index) {
						for(var i = 0; i < tr.length; i++) {
							var datass = {};
							$(tr[i]).find("input[name=person_name]").val(null);
							$(tr[i]).find("input[name=person_price]").val(null);
							$(tr[i]).find("input[name=person_num]").val(null);
							$(tr[i]).find("input[name=minorder_num]").val(null);
						}
						layer.close(index);
						layer.msg('清空成功');
					}
				});

			})

			//表单提交
			form.on('submit(admin)', function(data) {
				var specs = $(this).attr('date-bt');
				data = [];
				var tr = $('#yue' + specs + ' .tabto ._person tr');
				var goods = $('input[name="goods_id"]').val();
				var starttime = $('input[name="starttime"]').val();
				var endtime = $('input[name="endtime"]').val();
				var shu = [];

				var shu1 = new Object();
				for(var i = 0; i < tr.length; i++) {
					var datass = {};
					datass.person_name = $(tr[i]).find("input[name=person_name]").val();
					datass.person_price = $(tr[i]).find("input[name=person_price]").val();
					datass.person_num = $(tr[i]).find("input[name=person_num]").val();
					datass.minorder_num = $(tr[i]).find("input[name=minorder_num]").val();
					datass.person_id = $(tr[i]).find("input[name=person_id]").val();
					datass.specs_id = specs;
					datass.time = $(tr[i]).find("input[name=minorder_num]").attr('date-rq');

					shu.push(datass);
				}
				var	dates = JSON.stringify(shu);
				//				console.log(shu);

				layer.msg('确定提交当前选项卡的数据吗？', {
					time: 0 //不自动关闭
						,
					btn: ['确定', '取消'],
					yes: function(index) {
						//加载层-风格2
						layer.load(1);
						$.ajax({
							url: editurl,
							type: "post",
							dataType: "json",
							data: {
								goods:goods,
								starttime:starttime,
								endtime:endtime,
								dates
							},
							success: function(data) {
								//调用日历控件生成元素
								layer.closeAll('loading');
								layer.msg(data.msg)
								//							console.log(data)
							},
							error: function(jqXHR) {
								layer.msg(jqXHR);
							}
						});
						layer.close(index);
//						layer.msg('清空成功');
					}
				});

				return false;
			})

			//弹层单选按钮
			form.on('radio(radio)', function(data) {

				if(data.value == 3) {
					$('#chx').css('display', 'block')
					console.log(data.value)
				} else {

					$("input[type='checkbox']:checked").each(function() {

					})
					$('#chx').css('display', 'none')

				}

			});

			function clickOneday(_that)
			{
                var ri = _that.attr('date-ri');
                var specs = _that.attr('date-specs');

                $('.ri').removeClass('bj')
                _that.addClass('bj')
                $('#yue' + specs + ' #' + ri).siblings().hide();
                $('#yue' + specs + ' #' + ri).show();
                //点击时间弹框 取消自20190319-bywtt
                // layer.msg(ri + '号', {
                //     time: 700
                // });
			}
			//为表格添加点击事件
			$(document).on('click', '.ri', function() {
                clickOneday($(this))
			})

			//批量添加的按钮点击事件
			$(document).on('click', '.add', function() {
				var pid = $(this).attr('date-pid')
				$.post(posurl, {
					'specs_id': pid
				}, function(da) {
					var sr = ''
					$('.batch_person').html('');
					$.each(da, function(i, v) {
						sr += '<tr date-id="' + v.person_id + '">' +
							'<td><input type="text" name="person_name" id="person_name" class="layui-input" placeholder="' + v.person_name + '" value="' + v.person_name + '"/>' +
							'<input type="hidden" name="person_id" id="person_id" class="layui-input" placeholder="' + v.person_id + '" value="' + v.person_id + '"/></td>' +
							'<td><input type="text" name="person_price" id="person_price" class="layui-input" placeholder="' + v.person_name + '价格" value="' + v.person_price + '"/></td>' +
							'<td><input type="text" name="person_num" id="person_num" class="layui-input" placeholder="数量" value="' + v.person_num + '" /></td>' +
							'<td><input type="text" name="minorder_num" id="minorder_num" class="layui-input" placeholder="最低下单" value="' + v.minorder_num +'" /></td>' +
							'</tr>';
					})

					$('.batch_person').html(sr);

				})
				//打开一个弹窗
				layer.open({
					type: 1,
					area: ['700px', '500px'],
					btn: ['保存', '取消'],
					content: $('#ceshi'),
					yes: function(index, layero) {
						var shijian = $('.shijian');
						var starttime = $('#starttime').val() //获取开始时间
						var endtime = $('#endtime').val() //获取结束时间
						var k = parseInt(new Date(starttime + ' 00:00:00').getTime());
						var e = parseInt(new Date(endtime + ' 23:59:59').getTime());
						var day = '';

						var danxuan = $("input[name='radio']:checked").val();
						danxuan = parseInt(danxuan ? danxuan : 0)
						for(var i = 0; i < shijian.length; i++) {
							var dates = [];
							var kaishi = $(shijian[i]).find('input[name=t—starttime]').val();
							var end = $(shijian[i]).find('input[name=t—endtime]').val();
							var s = parseInt(new Date(kaishi + ' 00:00:00').getTime());
							var j = parseInt(new Date(end + ' 23:59:59').getTime());

							if(s < k) {
								layer.msg('时间段开始时间小于商品上架时间');
								return false;
							}
							if(j > e) {
								layer.msg('时间段结束时间大于商品下架时间');
								return false;
							}
							if(j < s) {
								layer.msg('结束时间不能小于开始时间');
								return false;
							}

							var da = getMonthAlls(kaishi, end, danxuan);
							
							day += da.join(',')
							if(shijian.length - 1 != i) {
								day += ',';
							}
						}
						day = day.split(',') //分割成数组
						shu = [];
						var tr = $('#yue' + pid + ' .tabto ._person tr');

						var gai = $('.batch_person tr');

						for(var i = 0; i < tr.length; i++) {
							for(var n = 0; n < day.length; n++) {
								if($(tr[i]).attr('date-trd') == day[n]) {

									for(var j = 0; j < gai.length; j++) {
										if($(gai[j]).attr('date-id') === $(tr[i]).attr('date-person')) {

											$(tr[i]).find("input[name=person_name]").val($(gai[j]).find("input[name=person_name]").val());
											$(tr[i]).find("input[name=person_price]").val($(gai[j]).find("input[name=person_price]").val());
											$(tr[i]).find("input[name=person_num]").val($(gai[j]).find("input[name=person_num]").val());
											$(tr[i]).find("input[name=minorder_num]").val($(gai[j]).find("input[name=minorder_num]").val());
										}
									}
								}
							}

						}
						layer.msg('修改完成');
						layer.close(index);
					}
				});

			})
			//判断日期组装数据
			function getMonthAlls(begin, end, danxuan) {
				var d1 = begin;
				var d2 = end;
				var dateArry = new Array();
				var s1 = d1.split("-");
				var s2 = d2.split("-");
				var duo = $("input[type='checkbox']:checked");
				var mCount = 0;
				if(parseInt(s1[0]) < parseInt(s2[0])) {
					mCount = (parseInt(s2[0]) - parseInt(s1[0])) * 12 + parseInt(s2[1]) - parseInt(s1[1]) + 1;
				} else {
					mCount = parseInt(s2[1]) - parseInt(s1[1]) + 1;
				}
				
				var sta= new Date(d1+' 00:00:00').getTime()-1000;
				var end= new Date(d2+' 23:59:59').getTime();
				if(mCount > 0) {
					var startM = parseInt(s1[1]);
					var startY = parseInt(s1[0]);
					for(var i = 0; i < mCount; i++) {
						var da = new Date(startY, startM, 0);
						
						
						
						//获取每月有多少天
						var day = da.getDate(); //获取当前月有多少天
						
						if(startM < 12) {

							for(j = 1; j <= day; j++) {
								
								var k = new Date(startY+'-'+startM+'-'+j+' 00:00:00').getTime();
								
								
								switch(danxuan) {
									case 1:
										if(j % 2 == 1) {
											if(k >= sta && k <= end) {
												dateArry.push(startY + "-" + (startM > 9 ? startM : "0" + startM) + "-" + (j > 9 ? j : "0" + j));
											}
										};
										break;
									case 2:
										if(j % 2 != 1) {
											if(k >= sta && k <= end) {
												dateArry.push(startY + "-" + (startM > 9 ? startM : "0" + startM) + "-" + (j > 9 ? j : "0" + j));
											}
										}
										break;
									case 3:

										if(k >= sta && k <= end) {
											var w = new Date(startY + '-' + startM + '-' + j)
											d = parseInt(w.getDay());
											duo.each(function() {
												if($(this).val() == d) {
													dateArry.push(startY + "-" + (startM > 9 ? startM : "0" + startM) + "-" + (j > 9 ? j : "0" + j));

												}
											})
										}
										break;
									default:

										if(k >= sta && k <= end) {
											dateArry.push(startY + "-" + (startM > 9 ? startM : "0" + startM) + "-" + (j > 9 ? j : "0" + j));
										}

										break;
								}

							}
							startM += 1;
						} else {

							for(j = 0; j < day; j++) {

								switch(danxuan) {
									case 1:
										if(j % 2 == 1) {
											if(k >= sta && k <= end) {
												dateArry.push(startY + "-" + (startM > 9 ? startM : "0" + startM) + "-" + (j > 9 ? j : "0" + j));
											}
										};
										break;
									case 2:
										if(j % 2 != 1) {
											if(k >= sta && k <= end) {
												dateArry.push(startY + "-" + (startM > 9 ? startM : "0" + startM) + "-" + (j > 9 ? j : "0" + j));
											}
										}
										break;
									case 3:

										if(k >= sta && k <= end) {
											var w = new Date(startY + '-' + startM + '-' + j)
											d = parseInt(w.getDay());
											duo.each(function() {
												if($(this).val() == d) {

													dateArry.push(startY + "-" + (startM > 9 ? startM : "0" + startM) + "-" + (j > 9 ? j : "0" + j));
												}
											})
										}
										break;
									default:
										if(k >= sta && k <= end) {
											dateArry.push(startY + "-" + (startM > 9 ? startM : "0" + startM) + "-" + (j > 9 ? j : "0" + j));
										}
										break;
								}

							}
							startM = 1;
							startY += 1;
						}
					}
				}
				return dateArry;
			}

			//获取hash来切换选项卡，假设当前地址的hash为data-id对应的值
			element.on('tab(test1)', function() {
				var starttime = $('#starttime').val() //获取开始时间

				var endtime = $('#endtime').val() //获取结束时间
				var specs_id = this.getAttribute('data-id'); //获取当前选项卡的规格id

				//判断当前选项卡下的类容是否存在元素 不存在就进行请求生成
				if($('#yue' + specs_id + ' #yue').is(":empty")) {
					$.ajax({
						url: posurl,
						type: "post",
						dataType: "json",
						data: {
							'specs_id': specs_id
						},
						success: function(data) {
							//调用日历控件生成元素
							yue(starttime, endtime, specs_id, data);
						},
						error: function(jqXHR) {
							console.log(jqXHR);
						}
					});
				}

			});
			function showSpecsData()
			{
                $('.Spec').html('') //清空容器
                var id = $('#goods_id').val() //获取当前商品的id
                var starttime = $('#starttime').val() //获取开始时间

                var endtime = $('#endtime').val() //获取结束时间
                if(starttime.length <= 0) {
                    layer.msg('开始时间不能为空');
                    return false;
                }
                if(endtime.length <= 0) {
                    layer.msg('结束时间不能为空');
                    return false;
                }

                //开始时间转化时间戳
                var k = parseInt(new Date(starttime + ' 00:00:00').getTime());
                //结束时间转化时间戳
                var j = parseInt(new Date(endtime + ' 23:59:59').getTime());
                //获取当前时间戳
                var d = parseInt(new Date().getTime());
                if(j < k) {
                    layer.msg('结束时间不能小于开始时间');
                    return false;
                }
                // console.log( (d - 24*60*60*1000))
                if(k < (d - 24*60*60*1000)) {
                    layer.msg('开始时间不能小于当前时间');
                    return false;
                }

                $.ajax({
                    url: specsurl,
                    type: "post",
                    dataType: "json",
                    data: {
                        id: id
                    },
                    success: function(s) {
                        if(s.code==-1){
                            layer.msg("请先添加套餐");
                            return false;
						}
						s= s.data;
                        var st = ''; //定义二级选项卡
                        var sp = ''; //定义二级选项卡类容

                        $('.content').html('')
                        $.each(s, function(i, v) {
                            //						console.log(i);

                            if(i == 0) {
                                st += '<li id="top' + v.specs_id + '" class="layui-this" data-id="' + v.specs_id + '">' + v.specs_name + '</li>'

                                sp += '<div class="layui-tab-item layui-show" id="yue' + v.specs_id + '">' +
                                    '<ul class="tab-ul">' +
                                    '<li class="tab-ul-title">' +
                                    '<div date-pid="' + v.specs_id + '" class="batch emptys">清空</div>' +
                                    '<div date-pid="' + v.specs_id + '" class="batch add layui-btn  layui-btn-normal">批量增加</div>' +
                                    '</li>' +

                                    '</ul>' +
                                    '<div class="layui-tab">' +
                                    '<ul class="layui-tab-title" id="yue">' +
                                    '</ul>' +
                                    '<div class="layui-tab-content tas" id="tas' + v.specs_id + '">' +
                                    '</div>' +
                                    '</div>' +

                                    '<div class="layui-form-item ">' +
                                    '<div class="layui-input-block admins">' +
                                    '<button date-bt="' + v.specs_id + '" class="layui-btn" lay-submit lay-filter="admin">立即提交</button>' +
                                    '</div>' +
                                    '</div>' +

                                    '</div>' +
                                    '</div>';

                            } else {
                                st += '<li id="top' + v.specs_id + '" data-id="' + v.specs_id + '">' + v.specs_name + '</li>'

                                sp += '<div class="layui-tab-item" id="yue' + v.specs_id + '">' +
                                    '<ul class="tab-ul">' +
                                    '<li class="tab-ul-title">' +
                                    '<div class="batch emptys">清空</div>' +
                                    '<div date-pid="' + v.specs_id + '" class="batch add layui-btn  layui-btn-normal">批量增加</div>' +
                                    '</li>' +
                                    '</ul>' +
                                    '<div class="layui-tab">' +
                                    '<ul class="layui-tab-title" id="yue">' +

                                    '</ul>' +
                                    '<div class="layui-tab-content tas" id="tas' + v.specs_id + '">' +

                                    '</div>' +
                                    '<div class="layui-form-item ">' +
                                    '<div class="layui-input-block admins">' +
                                    '<button date-bt="' + v.specs_id + '" class="layui-btn" lay-submit lay-filter="admin">立即提交</button>' +
                                    '</div>' +
                                    '</div>' +

                                    '</div>' +

                                    '</div>';

                            }

                            $('.Spec').html(st)
                            $('.content').html(sp)

                        })

                        addhtm(starttime, endtime, s );
                    },
                    error: function(jqXHR) {
                        console.log(jqXHR);
                    }
                });
			}
			//结束时间框失去焦点事件button
			$(document).on('click', '#button', function() {
				showSpecsData();
			})
			//渲染二级选项卡
			function addhtm(starttime, endtime, s ) {
				//发送post请求人群信息
				//默认请求第一次

				var specs_id = s[0].specs_id;
				console.log(specs_id)
				$.ajax({
					url: posurl,
					type: "post",
					dataType: "json",
					data: {
						'specs_id': specs_id
					},
					success: function(data) {

						yue(starttime, endtime, specs_id, data );
					},
					error: function(jqXHR) {
						console.log(jqXHR);
					}
				});
			}

			//			yue();
			//获取区间内的月份
			function yue(up, end, specs_id, person) {
				//				console.log(up)
				var da = getMonthAll(up, end);
				var yue = '';
				$('#tas' + specs_id).html('');
				for(var i = 0; i < da.length; i++) {
					var nian = da[i].split('-')
					if(i == 0) {
						yue += '<li class="layui-this" lay-id="' + i + '">' + nian[0] + '年' + nian[1] + '月</li>';
					} else {
						yue += '<li  lay-id="' + i + '">' + nian[0] + '年' + nian[1] + '月</li>';
					}
					var ta = datetime(da[i], i, person, specs_id, up, end);
					$('#tas' + specs_id).append(ta)
					//高亮第一个时间
                    $(".ri:first").click()
				}
				$('#yue' + specs_id + ' #yue').html('')
				$('#yue' + specs_id + ' #yue').prepend(yue)

			}
			//查询时间维度里的所有月份
			function getMonthAll(begin, end) {
				var d1 = begin;
				var d2 = end;
				var dateArry = new Array();
				var s1 = d1.split("-");
				var s2 = d2.split("-");
				var mCount = 0;
				if(parseInt(s1[0]) < parseInt(s2[0])) {
					mCount = (parseInt(s2[0]) - parseInt(s1[0])) * 12 + parseInt(s2[1]) - parseInt(s1[1]) + 1;
				} else {
					mCount = parseInt(s2[1]) - parseInt(s1[1]) + 1;
				}
				if(mCount > 0) {
					var startM = parseInt(s1[1]);
					var startY = parseInt(s1[0]);
					for(var i = 0; i < mCount; i++) {
						if(startM < 12) {
							dateArry[i] = startY + "-" + (startM > 9 ? startM : "0" + startM);
							startM += 1;
						} else {
							dateArry[i] = startY + "-" + (startM > 9 ? startM : "0" + startM);
							startM = 1;
							startY += 1;
						}
					}
				}
				return dateArry;
			}

			//渲染日历
			function datetime(up, d, person, specs_id, kaishi, jieshu) {

				var kaishi = kaishi.split("-");
				var jieshu = jieshu.split("-");
				var up = up.split("-");
				var da = new Date(up[0], up[1], 0);
				//获取每月有多少天
				var day = da.getDate(); //获取当前月有多少天
				da.setDate(1)
				var we = da.getDay(); //获取当前月的第一天是星期几
				var a = 0;
				var str = '';
				var tr = '';
				var tab = ''; ///每一天的tab
				if(d == 0) {
					tr += '<div class="layui-tab-item layui-show">';
				} else {
					tr += '<div class="layui-tab-item">';
				}
				tr += "<div class='layui-col-md4'>"
				tr += '<table class="layui-table">' +
					'<div class="titl">' + up[0] + '年' + up[1] +'月</div>'+
					'<thead>' +
					'<tr class="week-title">' +
					'<td>星期日</td>' +
					'<td>星期一</td>' +
					'<td>星期二</td>' +
					'<td>星期三</td>' +
					'<td>星期四</td>' +
					'<td>星期五</td>' +
					'<td>星期六</td>' +
					'</tr>' +
					'</thead>' +
					'<tbody class="tian">';

				for(var i = 1; i <= day + we; i++) {

					a++

					if(i > we) {
                        //按时间段渲染日历
                        var light = '';
                        var u = i - we
                        var str_class = up[0] + '-' + up[1] + '-' + (u > 9 ? u : '0' + u) + '">' + (u > 9 ? u : '0' + u);
                        var str_day = up[0] + '-' + up[1] + '-' + (u > 9 ? u : '0' + u);
                        if(str_day+','+specs_id in personprice){
                            light = 'light_star';
                        }
                        if(u < kaishi[2] && up[0] == kaishi[0] && up[1] == kaishi[1] || u > jieshu[2] && up[0] == jieshu[0] && up[1] == jieshu[1]) {
							str += '<td>' +
								'<div class="ri-w '+light+'" date-specs="' + specs_id + '" date-ri="' + str_class + '</div>' +
								'</td>';
						} else {
							str += '<td>' +
								'<div class="ri '+light+'" date-specs="' + specs_id + '" date-ri="' + str_class + '</div>' +
								'</td>';
						}

					} else {
						str += '<td></td>';
					}
					if(a == 7 || i == day + we) {
						tr += '<tr>' + str + '</tr>'

						str = '';
						a = 0

					}
					if(u === undefined) {

					} else {

						if(u < kaishi[2] && up[0] == kaishi[0] && up[1] == kaishi[1] || u > jieshu[2] && up[0] == jieshu[0] && up[1] == jieshu[1]) {

						} else {
						    var time = up[0] + '-' + up[1] + '-' + (u > 9 ? u : '0' + u);
							tab += '<div class="person_off" id="' + up[0] + '-' + up[1] + '-' + (u > 9 ? u : '0' + u) + '" style="display: none;">' +
								'<table class="layui-table">' +
								'<thead><tr><th>名称</th><th>价格</th><th>数量</th><th>最低下单数</th></tr></thead>' +
								'<tbody class="_person">';
							//循环人群
							//默认使用套餐数据填充 改用真实数据填充 2019-03-19
							var oldperson = list[time+","+specs_id];
							if(oldperson!==undefined && oldperson!==null){
							    person = oldperson;
							}else if(time+','+specs_id in personprice){
                                person = personprice[time+","+specs_id];
							}
                            for(var j = 0; j < person.length; j++) {

                                tab += '<tr  date-trd="' + up[0] + '-' + up[1] + '-' + (u > 9 ? u : '0' + u) + '" date-person="' + person[j].person_id + '" ><td><input type="hidden" name="person_id" date-aid="' + person[j].person_id + '" date-rq="' + up[0] + '-' + up[1] + '-' + (u > 9 ? u : '0' + u) + '" id="" class="layui-input" placeholder="成人价格" value="' + person[j].person_id + '" />' +
                                    '<input type="text" name="person_name" date-aid="' + person[j].person_id + '" date-rq="' + up[0] + '-' + up[1] + '-' + (u > 9 ? u : '0' + u) + '" id="" class="layui-input" placeholder="名称" value="' + person[j].person_name + '" /></td>' +
                                    '<td><input type="number" name="person_price" date-aid="' + person[j].person_id + '" date-rq="' + up[0] + '-' + up[1] + '-' + (u > 9 ? u : '0' + u) + '" id="" class="layui-input" placeholder="价格" value="' + person[j].person_price + '" /></td>' +
                                    '<td><input type="number" name="person_num" date-aid="' + person[j].person_id + '" date-rq="' + up[0] + '-' + up[1] + '-' + (u > 9 ? u : '0' + u) + '"  id="" class="layui-input" placeholder="数量" value="' + person[j].person_num + '" />' +
                                    '<input type="hidden" name="specs_id" id="" date-aid="' + person[j].person_id + '" date-rq="' + up[0] + '-' + up[1] + '-' + (u > 9 ? u : '0' + u) + '"  class="layui-input" placeholder="数量" value="' + person[j].specs_id + '" /></td>' +
                                    '<td><input type="number" name="minorder_num" date-aid="' + person[j].person_id + '" date-rq="' + up[0] + '-' + up[1] + '-' + (u > 9 ? u : '0' + u) + '"  id="" class="layui-input" placeholder="最低下单" value="' + person[j].minorder_num + '" /></td>' +
                                    '</tr>';
                            }
							tab +=
								'</tbody>' +
								'</table>' +
								'</div>';
						}
					}
				}

				tr += '</tbody>' +
					'</table>' +
					'</div>' +
					'<div class="layui-col-md1">&nbsp</div>' +
					'<div class="layui-col-md7 tabto">' + tab + '</div>' +
					'</div>';
				return tr;
			}
            //初始化商品列表的规格数据
            $(document).ready(function(){
                showSpecsData();
            })
		});
	</script>
	<script type="text/javascript">
        layui.use('layer', function(){
            var layer = layui.layer;
            $('.a_menu').click(function(){
                var url = $(this).attr('href');
                var a = 0;
                $.ajax({
                    url:url
                    ,async:false
                    ,success:function(res){
                        if(res.code == 0) {
                            layer.msg(res.msg);
                            a = 1;
                        }
                    }
                })
                if(a === 0) {
                    layer.open({
                        type:2,
                        content:url,
                        area: ['800px', '680px'],
                        end:function(){
                            location.reload();
                        }
                    });
                }
                return false;
            })

        });

	</script>
</html>